#ifdef KITSCHY_DEBUG_MEMORY 
#include "debug_memorymanager.h"
#endif

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"

#include "BList.h"
#include "BVector.h"
#include "quaternion.h"
#include "symbol.h"
#include "sort.h"
#include "ontology.h"
#include "featureterm.h"
#include "FTKBase.h"
#include "SpecialTerm.h"
#include "ST_floatinterval.h"

#include "AWLocalViewport.h"

#include "AWEntity.h"
#include "AWMessage.h"
#include "AWScenario.h"
#include "AWBody.h"
#include "AWSensor.h"
#include "AWBrain.h"
#include "AWAgent.h"
#include "AWNOOSAgent.h"
#include "AWWorld.h"

#include "loader.h"


AW_NOOSAgent::AW_NOOSAgent() : AW_Agent() {
	class_name->set("AW_NOOSAgent");

	CaseBase=new FTKBase(O);
	CaseBase->uses(Memory);
} /* AW_NOOSAgent::AW_NOOSAgent */ 


AW_NOOSAgent::AW_NOOSAgent(Ontology *o,FTKBase *m,FTKBase *cb,AW_Body *bdy,AW_Sensor *sens,AW_Brain *brn) : AW_Agent(o,m,bdy,sens,brn) {
	class_name->set("AW_NOOSAgent");

	CaseBase=cb;
} /* AW_NOOSAgent::AW_NOOSAgent */ 


AW_NOOSAgent::~AW_NOOSAgent() {
	delete CaseBase;
	CaseBase=0;
} /* AW_NOOSAgent::~AW_NOOSAgent */ 


AW_NOOSAgent::AW_NOOSAgent(FILE *fp) : AW_Agent()
{
	class_name->set("AW_NOOSAgent");
	Body=0;
	Sensor=0;
	Brain=0;
	AgentName=0;
	CaseBase=0;
	
	load(fp);
} /* AW_NOOSAgent */ 


bool AW_NOOSAgent::load(FILE *fp)
{
	if (!AW_Entity::load(fp)) return false;

	if (CaseBase!=0) delete CaseBase;
	if (Memory!=0) delete Memory;
	if (O!=0) delete O;

	O=new Ontology(fp);
	ST_floatInterval::createSort(O);

	Memory=new FTKBase(fp,O);
	CaseBase=new FTKBase(O);
	CaseBase->uses(Memory);
	CaseBase->load(fp,O);

	AgentName=new Symbol(fp);
	Body=(AW_Body *)ClassLoader(fp,O,Memory,(void *)CaseBase);
	Sensor=(AW_Sensor *)ClassLoader(fp,O,Memory);
	Brain=(AW_Brain *)ClassLoader(fp,O,Memory,(void *)CaseBase);

	return true;
} /* AW_NOOSAgent::load */ 


bool AW_NOOSAgent::save(FILE *fp)
{
	if (!AW_Entity::save(fp)) return false;

	if (!O->save(fp)) return false;
	if (!Memory->save(fp,0)) return false;
	if (!CaseBase->save(fp,Memory)) return false;

	AgentName->save(fp);
	if (!Body->save(fp)) return false;
	if (!Sensor->save(fp)) return false;
	if (!Brain->save(fp)) return false;

	return true;
} /* AW_NOOSAgent::save */ 



bool AW_NOOSAgent::is_a(char *c)
{
	if (strcmp(c,"AW_NOOSAgent")==0) return true;

	return AW_Entity::is_a(c);
} /* AW_NOOSAgent::is_a */ 


bool AW_NOOSAgent::is_a(Symbol *c)
{
	if (c->cmp("AW_NOOSAgent")) return true;

	return AW_Entity::is_a(c);
} /* AW_NOOSAgent::is_a */ 
